package cn.itcast.service.system.impl;

import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

 @Autowired
 private UserDao userDao;

 //分页查询
 public PageInfo findAll(String companyId, int page, int size) {
  //设置分页参数
  PageHelper.startPage(page,size);
  //调用dao查询
  List<User> list = userDao.findAll(companyId);
  //构造返回值PageInfo
  return new PageInfo(list);
 }

 //保存
 public void save(User user) {

  user.setId(UUID.randomUUID().toString());

  userDao.save(user);
 }

 //更新用户
 public void update(User user) {
  userDao.update(user);
 }

 //根据id查询用户
 public User findById(String id) {
  return userDao.findById(id);
 }

 //根据id删除用户
 public void delete(String id) {
  userDao.delete(id);
 }

 //查询此用户所有角色id
 public List<String> findRolesByUserId(String id) {
  return userDao.findRolesByUserId(id);
 }

 //对用户重新分配角色
 public void changeRole(String userId, String[] roleIds) {
  //调用dao根据id删除用户角色
  userDao.deleteUserRole(userId);
  //循环所有角色的数组
  for (String roleId : roleIds) {
   //调用dao向角色用户中间表中保存用户
   userDao.saveUserRole(userId,roleId);
  }
 }

 //根据邮箱查询用户
 public User findByEmail(String email) {
  return userDao.findByEmail(email);
 }
}
